package com.xxp.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.xxp.Exception.ServiceException;
import com.xxp.controller.DTO.LoginDto;
import com.xxp.controller.request.AdminPageRequest;
import com.xxp.controller.request.LoginRequest;
import com.xxp.controller.request.PassRequest;
import com.xxp.controller.request.UserPageRequest;
import com.xxp.entity.Admin;
import com.xxp.entity.User;
import com.xxp.mapper.AdminMapper;
import com.xxp.mapper.UserMapper;
import com.xxp.service.AdminService;
import com.xxp.service.UserService;
import com.xxp.util.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Security;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AdminServiceImpl implements AdminService {
    private static final String DEFAULT_PASS="123";
    private static final String PASS_SALT="xiaxp";

    @Autowired
    private AdminMapper adminMapper;
    //查询所有
    @Override
    public List<Admin> list() {
        List<Admin> admin = adminMapper.list();
        return admin;
    }
    //分页
    @Override
    public Object page(AdminPageRequest AdminPageRequest) {
        //分页：pom文件当中使用：pagehelper-spring-boot-starter
        //在需要分页的方法前后使用startPage和new PageInfo，然后就会对结果封装成一个分页的对象，然后返回前端，前端对分页的对象进行处理
        PageHelper.startPage(AdminPageRequest.getPageNum(),AdminPageRequest.getPageSize());
        List<Admin> admin = adminMapper.listByCondition(AdminPageRequest);
        //直接返回一个new pageInfo
        return new PageInfo<>(admin);

    }
    //新增
    @Override
    public void save(Admin admin) {
        String username = admin.getUsername();
        Admin byUsername = adminMapper.findByUsername(username);
        if (byUsername!=null){
            throw new ServiceException("用户名已存在");
        }
       //密码加密
        if (StrUtil.isBlank(admin.getPassword())){
            admin.setPassword(admin.getPassword());
        }
        //设置md5加密，加盐
        admin.setPassword(securePass(admin.getPassword()));
        admin.setIdentity(1);
        adminMapper.save(admin);
    }

    @Override
    public Admin getById(Integer id) {
        Admin admin=adminMapper.getById(id);
        return admin;
    }

    @Override
    public void update(Admin admin) {
        admin.setUpdatetime(new Date());
        adminMapper.update(admin);
    }

    @Override
    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }

    @Override
    public LoginDto login(LoginRequest loginRequest) {
            loginRequest.setPassword(securePass(loginRequest.getPassword()));
            Admin admin = adminMapper.getByUsernameAndPassword(loginRequest.getUsername(),loginRequest.getPassword());
            //判断查询出来的用户是否为空
            if (admin==null){
                //是：抛异常
                throw new ServiceException("当前用户不存在");
            }
            if (!admin.isStatus()){
                throw new ServiceException("当前用户已被禁用，请联系管理员");
            }
            LoginDto loginDto = new LoginDto();
            //把admin接收的数据拷贝到loginDto
            BeanUtils.copyProperties(admin,loginDto);
        //生成token
        String token = TokenUtils.genToken(String.valueOf(admin.getId()), admin.getPassword());
        loginDto.setToken(token);
        return loginDto;
    }

    @Override
    public void changePass(PassRequest passRequest) {
        //对传入的密码进行加密

        passRequest.setNewPass(securePass(passRequest.getNewPass()));
        int i = adminMapper.updatePassword(passRequest);
        if (i<=0){
            throw new ServiceException("修改失败");
        }
    }

    @Override
    public void changePassById(PassRequest passRequest) {
        //对旧密码加密比对
        passRequest.setPassword(securePass(passRequest.getPassword()));
        String oldPass= passRequest.getPassword();
        String username=passRequest.getUsername();
           //通过传过来的用户名和旧密码判断
        Admin byUsernameAndPassword = adminMapper.getByUsernameAndPassword( username,oldPass);
        //如果查出来的数据不为null，则设置新密码
        if (byUsernameAndPassword!=null){
            passRequest.setNewPass(securePass(passRequest.getNewPass()));
           int i= adminMapper.updatePassword(passRequest);
           if (i<=0){
               throw new ServiceException("修改失败");
           }
        }else{
            throw new ServiceException("旧密码不对");
        }


    }

    //密码加密
    private String securePass(String password){
        return SecureUtil.md5(password +PASS_SALT);
    }
}
